"""
@Author：WangYuXiang
@E-mile：Hill@3io.cc
@CreateTime：2021/4/2 16:19
@DependencyLibrary：无
@MainFunction：无
@FileDoc： 
    site_views.py
    文件说明
@ChangeHistory:
    datetime action why
    example:
    2021/4/2 16:19 change 'Fix bug'
        
"""

from sanic_rest_framework.mixins import ListModelMixin
from sanic_rest_framework.views import ModelViewSet


class AdminSiteViewListMixin(ListModelMixin):
    async def get_add_sources(self):
        return {}

    async def get_change_sources(self):
        return {}

    async def get_search_sources(self):
        return {}

    async def config_response(self, data):
        """
        返回给定输出数据的分页样式`Response`对象。
        """
        return {'recordData': data, 'searchData': await self.get_search_sources(), 'addData': await self.get_add_sources(), 'changeData': await self.get_change_sources()}

    def get_paginated_response(self, data):
        """
        返回给定输出数据的分页样式`Response`对象。
        """
        assert self.paginator is not None
        return {
            'count': self.paginator.count,
            'page': self.paginator.query_page,
            'page_size': self.paginator.query_page_size,
            'results': data,
        }

    async def list(self, request, *args, **kwargs):
        queryset = await self.get_queryset()

        page = await self.paginate_queryset(queryset)
        if page is not None:
            self.paginator.set_count(await self.get_paginator_count(queryset))
            serializer = self.get_serializer(page, many=True)
            data = self.get_paginated_response(await serializer.data)
            data = await self.config_response(data)
            return self.success_json_response(data=data)
        serializer = self.get_serializer(queryset, many=True)
        data = await self.config_response(await serializer.data)
        return self.success_json_response(data=data)


class AdminSiteView(AdminSiteViewListMixin, ModelViewSet):
    pass
